/*
 * Copyright 2007 The semap project development team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by myn at 07.08.2007 17:03:22.
 *
 * TODO <<insert description>>.
 *
 * $Id: StringMaker.java 382 2007-08-16 21:01:36Z falexg $
 */

package com.google.code.contactserv.common.makers;

import com.google.code.contactserv.model.PropertyHolder;

import java.util.List;

public class SPARQLMaker {

    public static String makeSPARQLReq(List<PropertyHolder> props) {
        StringBuffer sparqlReq = new StringBuffer();
        StringBuffer selectPart = new StringBuffer();
        StringBuffer wherePart = new StringBuffer();
        StringBuffer filterPart = new StringBuffer();
        StringBuffer unionPart = new StringBuffer();

        sparqlReq.append("PREFIX foaf: <http://xmlns.com/foaf/0.1/> ");
        selectPart.append("SELECT DISTINCT $person");
        wherePart.append(" WHERE { $person a foaf:Person;");

        for (PropertyHolder pair : props) {
            if (pair.getName().equals("url")) {
//                selectPart.append(" DISTINCT");
                unionPart.append(" { $person foaf:homepage ?url .} UNION { $person foaf:weblog ?url .} UNION");
                unionPart.append("{ $person foaf:workpage ?url .} UNION { $person foaf:openid ?url .}");
//            selectPart.append(" ?");
//            selectPart.append(pair.getName());
//            if (pair.isOptional()) {
//                optionalPart.append(" OPTIONAL { $person foaf:");
//                optionalPart.append(pair.getName());
//                optionalPart.append("  ?");
//                optionalPart.append(pair.getName());
//                optionalPart.append(" .}");
            } else {
                wherePart.append(" foaf:");
                wherePart.append(pair.getName());
                wherePart.append(" ?");
                wherePart.append(pair.getName());
                wherePart.append(";");
            }
            if (StringMaker.isRegEx(pair.getValue())) {
                filterPart.append(" FILTER regex(str(?");
                filterPart.append(pair.getName());
                filterPart.append("), \"");
                filterPart.append(StringMaker.cleanQuote(pair.getValue()));
                filterPart.append("\", \"i\").");
            } else {
                filterPart.append(" FILTER (str(?");
                filterPart.append(pair.getName());
                filterPart.append(") = \"");
                filterPart.append(pair.getValue());
                filterPart.append("\").");
            }
        }
        sparqlReq.append(selectPart);
        sparqlReq.append(wherePart);
        sparqlReq.append(".");
        sparqlReq.append(unionPart);
        sparqlReq.append(filterPart);
        sparqlReq.append("}");

// Removed  by ser.emel as non-essantial for the functional test purpose.
// Correct service behaviour is check by its output to the System.out
//        System.out.println(sparqlReq.toString());
        return sparqlReq.toString();
    }
}
